/**
 *
 * Copyright  2011 Nokia Corporation.
 * tuomo.hirvonen@digia.com
 *
 */


#include "thegame.h"
#include "themenu.h"


TheMenu::TheMenu(TheGame *g, const char *menuStrings[], const bool *ai ) {
    game = g;
    currentStrings = menuStrings;
    activeItems = ai;

    // Calculate items in menuStrings
    const char **m = currentStrings;
    itemCount = 0;
    activeItem = -1;
    while (*m!=0) { itemCount++;  m++; }
    appearCounter = -1.0f;
};


TheMenu::~TheMenu() {

}

int TheMenu::coordToMenuIndex( float x, float y ) {
    float startx = game->getSpriteBatch()->getTargetWidth()/2 - BUTTON_TEXT_SIZE*4;
    float endx = game->getSpriteBatch()->getTargetWidth()/2 + BUTTON_TEXT_SIZE*4;
    if (x<startx || x>endx ) return -1;

    float starty = game->getSpriteBatch()->getTargetHeight()*MENU_RELATIVE_Y_CENTER
                    - BUTTON_TEXT_SIZE*itemCount*MENU_Y_SPACING/2.0f - BUTTON_TEXT_SIZE*MENU_Y_SPACING/2.0f;

    return (int)( (y-starty) / (BUTTON_TEXT_SIZE*MENU_Y_SPACING) );
}

bool TheMenu::mousedown( float x, float y) {

    int clickIndex = coordToMenuIndex( x,y );

    if (clickIndex>=0 && clickIndex<itemCount) {
        /*
        if (activeItems && activeItems[clickIndex]==false) {
            activeItem = -1;
            return false;
        }
        */
        activeItem = clickIndex;
        return true;
    } else activeItem = -1;


    return false;
}


int TheMenu::mouseup( float x, float y ) {
    if (activeItem<0) return -1;
    int clickIndex = coordToMenuIndex(x,y);

    if (clickIndex == activeItem) {
        activeItem = -1;
        return clickIndex;
    } else {
        activeItem = -1;
        return -1;
    }
}


void TheMenu::update( const float frameTime ) {
    appearCounter += frameTime*6.0f;
}


void TheMenu::render( ) {
    SpriteBatch *batch = game->getSpriteBatch();

    for (int f=0; f<itemCount; f++) {

        float d = (float)f - appearCounter;
        if (d<0.0f) d = 0.0f;
        d*=d;
        float m = ((float)(f&1)-0.5f) * batch->getTargetWidth();

        bool ai = true;
        if (activeItems) ai = activeItems[f];

        renderButton(batch->getTargetWidth()/2 + m*d,
                     batch->getTargetHeight() * MENU_RELATIVE_Y_CENTER
                     - BUTTON_TEXT_SIZE*itemCount*MENU_Y_SPACING/2.0f
                     + f*BUTTON_TEXT_SIZE*MENU_Y_SPACING,
                     currentStrings[f], f==activeItem, ai );
    }

}


void TheMenu::renderButton( float x, float y, const char *text, bool pressed, bool active ) {
    SpriteDrawInfo sdi;

    int l = strlen( text );

    sdi.textureHandle = game->getGameTextures().iconTexture;
    sdi.setSourceRect(0,0.5f, 0.5f, 0.5f );
    sdi.setTargetPos(x,y);

    float c = 1.0f;

    //if (!active) pressed=false;
    if (pressed) c = 0.5f;


    sdi.setScale( 8.6f * BUTTON_TEXT_SIZE * (0.9f+c/8.0f), BUTTON_TEXT_SIZE*MENU_Y_SIZE * (0.9f+c/8.0f));
    if (active==false) sdi.a = 0.5f;
    game->getSpriteBatch()->draw( &sdi );

    float col[4];
    col[0] = c; col[1] = c; col[2] = c; col[3] = 1.0 - c/8.0f;
    if (!active) col[3] = 0.25f;
    game->writeText( game->getSpriteBatch(), text, x-l*BUTTON_TEXT_SIZE*FONT_X_INC_MUL/2 + BUTTON_TEXT_SIZE/2,y, BUTTON_TEXT_SIZE, col );
}


